<template>
        <div>
     <mu-appbar title="货车兄弟">
     </mu-appbar>

     <mu-raised-button 
        label="fetch" 
        v-on:click="handleFetchMethod"
        class="login-raised-button" 
        primary/>

        <mu-snackbar v-if="snackbar" :message="toast" action="关闭" />
    </div>
</template>


<style>
  .login-raised-button {
    margin-top:1rem;
    width:12rem;
    }

   
</style>

<!--
    store.commit(types.LOGOUT);
                    router.replace({
                        path: 'login',
                        query: {redirect: router.currentRoute.fullPath}
                    })
                    -->
<script>
function checkStatus (response) {
  console.log('error response 1-->' + response.status)
  if (response.status >= 200 && response.status < 300) {
    return response
  } else if (response.status === 401) {
    console.log('401 auth error')
  } else {
    var error = new Error(response.statusText)
    error.response = response
    throw error
  }
}

function parseJSON (response) {
  return response.json()
}

export default {
  data () {
    return {
      snackbar: false,
      toast: ''
    }
  },
  methods: {
    handleFetchMethod () {
     // var obj = JSON.parse(jsonText)
     // credentials=omit same-origin include
     // mode: same-origin 'cors', img css -->no-cors https://segmentfault.com/a/1190000008484070
     // http://192.168.250.237/cros.html
     // http://www.ruanyifeng.com/blog/2016/04/cors.html
      var self = this
      fetch('http://test.dadachefu.com/truck/version/update', {
        mode: 'cors',
        method: 'post',
        credentials: 'omit',
        body: JSON.stringify({
          clientType: '1',
          osType: '1',
          versionCode: '1'
        })
      })
      .then(checkStatus)
      .then(parseJSON)
      .then(function (data) {
        console.log('error-->11111111')
       // self.handlerToast(data.data.content)
        self.handlerToast('用户你好,版本升级了')
      }
     ).catch(function (e) {
       console.log('error-->' + e)
     })
    },
    handlerToast (data) {
      this.toast = data
      this.snackbar = true
      if (this.snackTimer) clearTimeout(this.snackTimer)
      this.snackTimer = setTimeout(() => { this.snackbar = false }, 2000)
    }
  }
}
</script>